 package server.events;

 import com.alibaba.druid.pool.DruidPooledConnection;
 import database.DatabaseConnection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.HashMap;
 import java.util.Map;
 import tools.Pair;
 import tools.Randomizer;



 public class MapleOxQuizFactory
 {
/*  17 */   private static final MapleOxQuizFactory instance = new MapleOxQuizFactory();
/*  18 */   private final Map<Pair<Integer, Integer>, MapleOxQuizEntry> questionCache = new HashMap<>();

   public MapleOxQuizFactory() {
/*  21 */     initialize();
   }

   public static MapleOxQuizFactory getInstance() {
/*  25 */     return instance;
   }

   public Map.Entry<Pair<Integer, Integer>, MapleOxQuizEntry> grabRandomQuestion() {
/*  29 */     int size = this.questionCache.size();
     while (true) {
/*  31 */       for (Map.Entry<Pair<Integer, Integer>, MapleOxQuizEntry> oxquiz : this.questionCache.entrySet()) {
/*  32 */         if (Randomizer.nextInt(size) == 0) {
/*  33 */           return oxquiz;
         }
       }
     }
   }

   private void initialize() {
/*  40 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/*  41 */       PreparedStatement ps = con.prepareStatement("SELECT * FROM wz_oxdata");
/*  42 */       ResultSet rs = ps.executeQuery();
/*  43 */       while (rs.next()) {
/*  44 */         this.questionCache.put(new Pair(Integer.valueOf(rs.getInt("questionset")), Integer.valueOf(rs.getInt("questionid"))), get(rs));
       }
/*  46 */       rs.close();
/*  47 */       ps.close();
/*  48 */     } catch (Exception e) {
/*  49 */       e.printStackTrace();
     }
   }

   private MapleOxQuizEntry get(ResultSet rs) throws SQLException {
/*  54 */     return new MapleOxQuizEntry(rs.getString("question"), rs.getString("display"), getAnswerByText(rs.getString("answer")), rs.getInt("questionset"), rs.getInt("questionid"));
   }

   private int getAnswerByText(String text) {
/*  58 */     if (text.equalsIgnoreCase("x"))
/*  59 */       return 0; 
/*  60 */     if (text.equalsIgnoreCase("o")) {
/*  61 */       return 1;
     }
/*  63 */     return -1;
   }


   public static class MapleOxQuizEntry
   {
     private final String question;
     private final String answerText;
     private final int answer;
     private final int questionset;
     private final int questionid;

     public MapleOxQuizEntry(String question, String answerText, int answer, int questionset, int questionid) {
/*  76 */       this.question = question;
/*  77 */       this.answerText = answerText;
/*  78 */       this.answer = answer;
/*  79 */       this.questionset = questionset;
/*  80 */       this.questionid = questionid;
     }

     public String getQuestion() {
/*  84 */       return this.question;
     }

     public String getAnswerText() {
/*  88 */       return this.answerText;
     }

     public int getAnswer() {
/*  92 */       return this.answer;
     }

     public int getQuestionSet() {
/*  96 */       return this.questionset;
     }

     public int getQuestionId() {
/* 100 */       return this.questionid;
     }
   }
 }


/* Location:              C:\PGX\Server.jar!\server\events\MapleOxQuizFactory.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */